import 'dart:io';
import 'dart:math';

class m_utils {
  static String formartDateTime(DateTime date) {
    return "${date.year.toString()}-${date.month.toString().padLeft(2, '0')}-${date.day.toString().padLeft(2, '0')} ${date.hour.toString().padLeft(2, '0')}:${date.minute.toString().padLeft(2, '0')}";
  }

  static DateTime parseDateTime(String timeStr) {
    return DateTime.parse(timeStr);
  }

  static String formartDateTimeToSecond(DateTime date) {
    return "${date.year.toString()}${date.month.toString().padLeft(2, '0')}${date.day.toString().padLeft(2, '0')}_${date.hour.toString().padLeft(2, '0')}:${date.minute.toString().padLeft(2, '0')}:${date.second.toString().padLeft(2, '0')}";
  }

  static String timeDifferenceToNow(DateTime oldTime, DateTime nowTime) {
    var difference = nowTime.difference(oldTime);
    // print(oldTime.toString() + "|" + nowTime.toString());
    // print(difference);
    if (difference.inMinutes <= 3) {
      return '刚刚';
    } else if (difference.inMinutes > 3 && difference.inMinutes < 60) {
      return (difference.inMinutes).toString() + '分钟前';
    } else if (difference.inHours < 48 && nowTime.day - oldTime.day == 1) {
      return '昨天';
    } else if (difference.inHours >= 1 && difference.inHours < 24) {
      return (difference.inHours).toString() + '小时前';
    } else {
      return (difference.inDays + 1).toString() + '天前';
    }
  }

  static String numToString(int n) {
    if (n < 10000) {
      return n.toString();
    } else if (n < 1000000) {
      var str = (n / 10000).toString();
      return str.substring(0, str.lastIndexOf(".") + 3) + "w";
    } else {
      return ">100w";
    }
  }

  static String getFileType(String path) {
    int index = path.lastIndexOf(".");
    if (index < 0 || index + 1 > path.length) {
      return "";
    } else {
      return path.substring(index + 1);
    }
  }

  static String newLen(int strlenght) {
    String alphabet =
        'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
    String len = '';
    for (var i = 0; i < strlenght; i++) {
//    right = right + (min + (Random().nextInt(max - min))).toString();
      len = len + alphabet[Random().nextInt(alphabet.length)];
    }
    return len;
  }

  static String encodeStringArraytoJson(List<String> list) {
    String result = "[";
    for (var i = 0; i < list.length; i++) {
      if (i == list.length - 1) {
        result += "\\\"" + list[i] + "\\\"";
      } else {
        result += "\\\"" + list[i] + "\\\",";
      }
    }
    result += "]";
    return result;
  }

  static String subFileNameFromPath(String path) {
    int index = path.lastIndexOf("/");
    if (index < 0 || index + 1 > path.length) {
      return "";
    } else {
      return path.substring(index + 1);
    }
  }

  static Future<List<int>> fileRead(String path) async {
    return await new File(path).readAsBytes();
  }
}
